﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Fine.CRM.Common;

/// <summary>
/// Provides various configuration settings used by the application. Settings are read in from a CRM xml data resource defined in 
/// this class. Makes use of the configuration resource cache linked to the local context of the plugin or workflow step to access
/// the data.
/// </summary>
public class AzureSettings
{
    const string AZURE_PLACEHOLDER_TEXT = "Moved To Azure";
    const string AZURE_FILENAME_PREFIX = "(azure)";

    const string CONFIG_RESOURCE_NAME = "fine_/configurationdata/azureattachments.xml";
    const string CONFIG_PARAM_ACCOUNTNAME = "AccountName";
    const string CONFIG_PARAM_ACCOUNTKEY = "AccountKey";
    const string CONFIG_PARAM_CONTAINERNAME = "EmailAttachmentContainer";
    const string CONFIG_PARAM_FILEPREFIX = "FilePrefix";
    const string CONFIG_PARAM_PLACEHOLDERTEXT = "PlaceHolderText";

    //setting values that this class provides
    public string ContainerName { get; private set; }
    public string AccountName { get; private set; }
    public string AccountKey { get; private set; }
    public string FileNamePrefix { get; private set; }
    public string FilePlaceHolderText { get; private set; }

    public AzureSettings(XmlConfigurationResource config)
    {
        //defaults for filename prefix and placeholder text.
        this.FileNamePrefix = AZURE_FILENAME_PREFIX;
        this.FilePlaceHolderText = AZURE_PLACEHOLDER_TEXT;

        XDocument xmlConfiguration = config.GetXmlConfigurationDoc(CONFIG_RESOURCE_NAME);
        
        //loop through each config node in the configuration data and find the name and value
        //parameters. Copy to the appropriate attribute of the class
        foreach (XElement el in xmlConfiguration.Descendants("config"))
        {
            string name = string.Empty;
            string value = string.Empty;
            foreach (XAttribute attr in el.Attributes())
            {
                switch (attr.Name.LocalName)
                {
                    case "name":
                        name = attr.Value;
                        break;
                    case "value":
                        value = attr.Value;
                        break;
                }
            }

            //set the appropriate attribute of the class based on the setting name in the config file.
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
            {
                if (name == CONFIG_PARAM_ACCOUNTNAME)
                    this.AccountName = value;
                else if (name == CONFIG_PARAM_ACCOUNTKEY)
                    this.AccountKey = value;
                else if (name == CONFIG_PARAM_CONTAINERNAME)
                    this.ContainerName = value;
                else if (name == CONFIG_PARAM_FILEPREFIX)
                    this.FileNamePrefix = value;
                else if (name == CONFIG_PARAM_PLACEHOLDERTEXT)
                    this.FilePlaceHolderText = value;
            }

        }

        //throw an error if one of the azure connection parameters is missing.
        if (string.IsNullOrEmpty(this.AccountName)
            || string.IsNullOrEmpty(this.AccountKey)
            || string.IsNullOrEmpty(this.ContainerName))
        {
            throw new Exception("One or more of the required Azure connection parameters is missing from the configuration resource.");
        }

        //add a whitespace at the end of the prefix.
        this.FileNamePrefix += " ";

    }
}



